package com.qymerge.manage.core.utils;

import com.qymerge.manage.core.enums.EnvTypes;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created by qiuchen on 2015/9/9.
 */
@Component
public class Env {

    public static boolean isProduct;
    public static EnvTypes env;
    public static String apiDomain;
    public static String mDomain;
    public static String mnsapiDomain;

    // 备份链接数据库字段
    public static String dbback_hostip;
    public static String dbback_port;
    public static String dbback_username;
    public static String dbback_password;
    public static String dbback_save_path;
    public static String dbback_databasename;

    @PostConstruct
    public void init() {
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("user.properties");
        Properties p = new Properties();
        try {
            p.load(inputStream);
        } catch (Exception e1) {
            e1.printStackTrace();
        }

        env = EnvTypes.get(p.getProperty("env"));
        isProduct = Boolean.parseBoolean(p.getProperty("is.product"));
        apiDomain = isProduct ? "https://api.fexie.com.cn" : "https://api.fexie.com.cn";
        mDomain  = isProduct ? "http://m.zsmy.cn" : "http://test.m.zsmy.cn";
        mnsapiDomain = isProduct ? "http://mns.api.zsmy.cn" : "http://test.mns.api.zsmy.cn";


        // 备份数据库初始化参数
        dbback_hostip = p.getProperty("dbback.hostip");
        dbback_port = p.getProperty("dbback.port");
        dbback_username = p.getProperty("dbback.username");
        dbback_password = p.getProperty("dbback.password");
        dbback_save_path = isProduct ? p.getProperty("dbback.save_path_linux") : p.getProperty("dbback.save_path_windows");
        dbback_databasename = p.getProperty("dbback.databasename");

        MyLogger.log.debug(String.format("env: %s,product : %s", env.getEnv(), isProduct));

        try {
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
